package org.framework.common.api.support.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.framework.common.ext.util.ClassUtil;
import org.framework.common.ext.util.JsonUtil;
import org.springframework.ui.ModelMap;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

@JsonAutoDetect
public class FormModel implements Serializable{

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = 7342979099633143792L;
	
	/* 实体类
	 * eg: ajax data : {"entityClass":"com.app.miwawa.api.esm.entity.Adviser"}
	 */
	private String entityClass;
	
	private ModelMap bean;
	
	private List<ModelMap> beans;
	
	public <T> T getEntity() {
		String json = JsonUtil.serialize(bean);
		return JsonUtil.getBean(json, ClassUtil.getClass(entityClass));
	}
	
	public <T> List<T> getEntities() {
		List<T> entities = new ArrayList<T>();
		if(beans != null && !beans.isEmpty()){
			for (ModelMap map : beans) {
				String json = JsonUtil.serialize(map);
				T entity = JsonUtil.getBean(json, ClassUtil.getClass(entityClass));
				entities.add(entity);
			}
		}
		return entities;
	}
	
	public String getEntityName(){
		return ClassUtil.getClass(entityClass).getSimpleName();
	}
	
	public <T> Class<T> getEntityClass() {
		return ClassUtil.getClass(entityClass);
	}

	public void setEntityClass(String entityClass) {
		this.entityClass = entityClass;
	}

	public ModelMap getBean() {
		return bean;
	}

	public void setBean(ModelMap bean) {
		this.bean = bean;
	}

	public List<ModelMap> getBeans() {
		return beans;
	}

	public void setBeans(List<ModelMap> beans) {
		this.beans = beans;
	}
	
}
